package com.deepblue.image.maker.config;

import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.RegistryAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @ClassName DockerConfig
 * @Description
 * @Author yangxd
 * @Date 2021/9/1 14:27
 * @Version 1.0
 **/
@Configuration
@Slf4j
public class DockerConfig {


    @Resource
    private DockerConfigProperties dockerConfigProperties;

    /**
     * dockerClient
     *
     * @return
     */
    @Bean
    public DockerClient evnDockerClient() throws DockerCertificateException {
        DefaultDockerClient dockerClient = DefaultDockerClient.fromEnv()
                .connectionPoolSize(dockerConfigProperties.getPoolSize())
                .build();
        try {
            dockerClient.auth(registryAuth());
        } catch (DockerException | InterruptedException e) {
            e.printStackTrace();
            log.error("docker auth error",e);
        }
        return dockerClient;
    }

    @Bean
    public RegistryAuth registryAuth(){
        RegistryAuth registryAuth = RegistryAuth.builder()
                .username(dockerConfigProperties.getHarbor().getUserName())
                .password(dockerConfigProperties.getHarbor().getPassword())
                .build();
        return registryAuth;
    }
}
